package org.ling.xuan.table;


import com.intellij.util.ui.ColumnInfo;
import com.intellij.util.ui.ListTableModel;
import org.jetbrains.annotations.Nullable;
import org.ling.xuan.dao.MarkFileDao;
import org.ling.xuan.entity.mo.MarkFile;

import java.util.List;

public class MarkFileTableModel extends ListTableModel<MarkFile> {
    private final List<MarkFile> markFileList;
    private static final String[] columnNames = {"ID", "Project Name", "File Name", "Relation", "File Path", "CTD Time","Remark"};
    private static final ColumnInfo[] columnInfos =new ColumnInfo[columnNames.length];
    static {
        for (int i = 0; i < columnNames.length; i++) {
            String s = columnNames[i];
            ColumnInfo<MarkFile,String> columnInfo = new ColumnInfo<>(s) {
                @Override
                public @Nullable String valueOf(MarkFile localAdd) {
                    return localAdd.getId();
                }
                @Override
                public boolean isCellEditable(MarkFile localAdd) {
                    return s.equals("Remark");
                }
                public void setValue(MarkFile item, String remark) {
                    if(s.equals("Remark")) {
                        MarkFileDao.saveRemark(item.getId(), remark);
                    }
                }
            };
            columnInfos[i]=columnInfo;
        }
    }
    
    public MarkFileTableModel(List<MarkFile> markFileList) {
        super(columnInfos,markFileList);
        this.markFileList = markFileList;
    }

    @Override
    public int getRowCount() {
        return markFileList.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int column) {
        if(column<columnNames.length) {
            return columnNames[column];
        }else{
            return super.getColumnName(column);
        }
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        MarkFile markFile = markFileList.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return markFile.getId();
            case 1:
                return markFile.getProjectName();
            case 2:
                return markFile.getFileName();
            case 3:
                return markFile.isRelation();
            case 4:
                return markFile.getFilePath();
            case 5:
                return markFile.getCtdTime();
            case 6:
                return markFile.getRemark();
            default:
                return null;
        }
    }

    public void removeRow(int rowIndex) {
        markFileList.remove(rowIndex);
        fireTableRowsDeleted(rowIndex, rowIndex);
    }
}

